﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;

namespace MvvmTreeView
{
    public enum ContextMenuItemType
    {
        NotSet,
        Separator,
        Create,
        Edit,
        View,
        Action,
        Delete,
    }

    public delegate void MenuCheckChangedHandler(object sender, MenuCheckedEventArgs args);

    public class MenuCheckedEventArgs : EventArgs
    {
        public object Menu { get; set; }
        public object MenuItem { get; set; }
        public object Checkbox { get; set; }
        public string CheckboxGroup { get; set; }
        public bool IsChecked { get; set; }
    }

    public interface IContextMenuItem
    {
        List<IContextMenuItem> Items { get; set; }
        ContextMenuItemType ContextMenuItemType { get; set; }
    }

    public interface ICheckBoxContextMenuItem
    {
        bool IsChecked { get; set; }
        string CheckBoxGroup { get; set; }
        event MenuCheckChangedHandler MenuCheckChanged;
        MenuCheckChangedHandler CheckChanged { set; }
        void RaiseMenuCheckChanged(object sender, MenuCheckedEventArgs args);
    }

    public class ContextMenu : INotifyCollectionChanged, INotifyPropertyChanged, IContextMenuItem
    {
        private List<IContextMenuItem> items;
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        public string AccessShortcut { get; set; }
        public object Tag { get; set; }
        public ContextMenuItemType ContextMenuItemType { get; set; } 

        public ContextMenu()
        {
        }

        public ContextMenu(string accessShortcut)
        {
            this.AccessShortcut = accessShortcut;
        }

        public List<IContextMenuItem> Items
        {
            get
            {
                if (items == null)
                {
                    items = new List<IContextMenuItem>();
                }

                return items;
            }

            set
            {
                items = value;
            }
        }
    }

    public class ContextMenuItem : INotifyCollectionChanged, INotifyPropertyChanged, IContextMenuItem
    {
        private List<IContextMenuItem> subItems;
        public RoutedEventHandler ClickHandler { get; set; }
        public string Text { get; set; }
        public string Tag { get; set; }
        public string IconUrl { get; set; }
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public event PropertyChangedEventHandler PropertyChanged;
        public ContextMenuItemType ContextMenuItemType { get; set; } 

        public ContextMenuItem()
        {
        }

        public ContextMenuItem(string iconUrl, string text, string tag, RoutedEventHandler clickHandler)
        {
            this.IconUrl = iconUrl;
            this.Text = text;
            this.Tag = tag;
            this.ClickHandler = clickHandler;
        }

        public ContextMenuItem(string iconUrl, string text, string tag, ContextMenuItemType type, RoutedEventHandler clickHandler)
        {
            this.IconUrl = iconUrl;
            this.Text = text;
            this.Tag = tag;
            this.ClickHandler = clickHandler;
        }

        public List<IContextMenuItem> Items
        {
            get
            {
                if (subItems == null)
                {
                    subItems = new List<IContextMenuItem>();
                }

                return subItems;
            }

            set
            {
                subItems = value;
            }
        }
    }

    public class ContextMenuSeparator : IContextMenuItem
    {
        public ContextMenuItemType ContextMenuItemType { get; set; }

        public ContextMenuSeparator()
        {
            ContextMenuItemType = ContextMenuItemType.Separator;
        }

        public List<IContextMenuItem> Items
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
